package com.tabjin.rfidsocket.controller.v1.access;

import com.tabjin.rfidsocket.common.enums.CardStatusEnum;
import com.tabjin.rfidsocket.common.utils.PagedGridResult;
import com.tabjin.rfidsocket.common.utils.TabJsonResult;
import com.tabjin.rfidsocket.pojo.ArchCard;
import com.tabjin.rfidsocket.pojo.bo.access.ArchCardBO;
import com.tabjin.rfidsocket.pojo.bo.access.ArchIssueCardBO;
import com.tabjin.rfidsocket.service.access.ArchCardService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

/**
 * @author tabjin
 * create at 2021/1/21 11:45
 * @program rfid_socket
 * @description
 */
@RestController
@RequestMapping(value = "/access/card")
public class CardController {

    private final ArchCardService archCardService;

    public CardController(ArchCardService archCardService) {
        this.archCardService = archCardService;
    }

    /**
     * 发卡
     *
     * @param archIssueCardBO
     * @return
     */
    @RequiresPermissions(value = {"/access/card/add"}, logical = Logical.OR)
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult add(@RequestBody ArchIssueCardBO archIssueCardBO) {
        archCardService.add(archIssueCardBO);
        return TabJsonResult.ok();
    }

    @RequiresPermissions(value = {"/access/card/delete"}, logical = Logical.OR)
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult delete(@RequestParam Integer id) {
        archCardService.delete(id);// 软删除
        return TabJsonResult.ok();
    }

    /**
     * 删除卡片
     *
     * @param id
     * @return
     */
    @RequiresPermissions(value = {"/access/card/delete"}, logical = Logical.OR)
    @RequestMapping(value = "/loss", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult loss(@RequestParam Integer id) {
        archCardService.updateCardStatus(id, CardStatusEnum.LOSS.getValue());// 软删除
        return TabJsonResult.ok();
    }

    @RequiresPermissions(value = {"/access/card/update"}, logical = Logical.OR)
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult update(@RequestBody ArchCardBO archCardBO) {
        ArchCard result = archCardService.update(archCardBO);
        return TabJsonResult.ok(result);
    }

    /**
     * 获取所有卡片
     *
     * @param page
     * @param pageSize
     * @return
     */
    @RequiresPermissions(value = {"/access/card/list"}, logical = Logical.OR)
    @RequestMapping(value = "/allPaged", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult all(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PagedGridResult result = archCardService.getAll(page, pageSize);
        return TabJsonResult.ok(result);
    }

}
